// simple fragment shader

uniform sampler2D sampler;
uniform bool textured;
uniform float sRepeat;
uniform float tRepeat;
uniform float centerX;
uniform float centerY;
uniform float centerZ;

varying vec3 vertex;
varying vec3 position;
varying vec3 normal;
varying vec3 light;

const float pi = 3.1415;

vec2 getST()
{
    vec3 center = vec3(centerX, centerY, centerZ);
    vec3 r = normalize(vertex - center);
    float alpha = asin(r.y);
    float beta = atan(r.x, r.z);
    return vec2((beta/(2.0*pi)) * sRepeat, (alpha/pi+0.5) * tRepeat);
}

void main()
{
    vec4 color = textured ? texture2D(sampler, getST()) : gl_Color;
    // Afegim 0.30 perque hi hagi una mica de llum a la zona oposada al focus
    // Aixi nomès les ombres seran 100% negres
    float light = max(dot(normal, normalize(light - position)) + 0.2, 0.0) + 0.1;
    gl_FragColor = color * light;
}
